using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.Domain.Model;
using SapaFinance.Domain.Specifications;
using SapaFinance.GenericRepository;

namespace SapaFinance.OOMap.Helpers
{
    public class AccountToSelectListConvert : TypeConverter<Account, IEnumerable<SelectListItem>>
    {
        private readonly IGenericRepository<Account, Guid> _repository;

        public AccountToSelectListConvert(IGenericRepository<Account,Guid> repository)
        {
            _repository = repository;
        }

        protected override IEnumerable<SelectListItem> ConvertCore(Account source)
        {
            if (source == null)
            {
                var all = _repository.GetAll();
                return all.Select(x => new SelectListItem
                                       {
                                           Value = x.UID.ToString(),
                                           //Text = x.Number,
                                           Text = x.Bank.Name+","+x.Currency.Name
                                       });
            }
            var list = _repository.Specify<IAccountSpecification>().WithCorporation(source.Corporation).Active().ToResult().ToList();
            return list.Select(x => new SelectListItem
                                   {
                                       Value = x.UID.ToString(),
                                       //Text = x.Number,
                                       Text = x.Bank.Name+","+x.Currency.Name,
                                       Selected = source.UID == x.UID
                                   });
        }

    }
}